package com.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.pojo.User;
import com.test.utils.JdbcUtils;
import com.test.utils.Msg;
import com.test.utils.PagesMap;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class SeluserServlet
 * 根据用户名查询用户信息
 */
public class SeluserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SeluserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("我进来了。。。。");
		
		response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String username1 = request.getParameter("username");
        
        System.out.println("username:"+username1);
        List<User> list = new ArrayList<User>();
        JdbcUtils jdbcUtils = new JdbcUtils();
        try {
			Connection conn = jdbcUtils.getConnection();
			Statement stmt = conn.createStatement();
			String sql = "select * from user where username = '"+username1+"'";
			ResultSet result =  stmt.executeQuery(sql); //执行sql语句
			User user = null;
			while(result.next()) {
				user = new User();
				int id = result.getInt("id");
				String username = result.getString("username");
				String password = result.getString("password");
				user.setId(id);
				user.setUsername(username);
				user.setPassword(password);
				list.add(user);
			}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        Map<String,Object> pageInfo = PagesMap.getpagesUserMaps(list, 1);
        out.println(JSONObject.fromObject(Msg.success().add("list", pageInfo)));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
